using System;
using System.Xml;

using ECCIT.Configuration;

namespace ECCIT.Web.Configuration
{
	/// <summary>
	/// 
	/// </summary>
	public class BindingAssembliesManager : ConfigManager
	{
		private XmlDocument _configDoc;
		private string _appVirtualPath;

		public BindingAssembliesManager(){}

		public BindingAssembliesManager(string appVirtualPath) : base(appVirtualPath, "BindingAssemblies.config")
		{
			_appVirtualPath = appVirtualPath;
			_configDoc = GetConfigDocument();
		}

		public BindingAssembly GetAssembly(string key)
		{
			string path = string.Empty;
			string className = string.Empty;
			string ajaxNS = string.Empty;

			XmlNode node = _configDoc.DocumentElement.SelectSingleNode("./bindingAssembly[@key='" + key + "']");
            if (node == null)
                return null;

            XmlNode pathNode = node.Attributes.GetNamedItem("path");
            XmlNode classNode = node.Attributes.GetNamedItem("className");
            XmlNode ajaxNode = node.Attributes.GetNamedItem("ajaxNamespace");
            if (pathNode != null)
                path = pathNode.InnerText;

            if (classNode != null)
                className = classNode.InnerText;

            if (ajaxNode != null)
                ajaxNS = ajaxNode.InnerText;

			if( path.IndexOf("%installPath%\\") != -1 )
				path = path.Replace("%installPath%\\", _appVirtualPath);

			BindingAssembly assembly = new BindingAssembly(key, path, className, ajaxNS);
			return assembly;
		}
	}
}
